# Archlive <http://archlive-pkg.googlecode.com>
#
###################################################################
# To use this PKGBUILD:
# Firstly:  makepkg -g >>./PKGBUILD
# Then:   time makepkg 2>&1 | tee ./log
#          Use this command can output all the log to the file ./log
###################################################################

####################### Custom options#################
kver_file=kernel26.kver
preset_file=kernel26.preset
kernel_name=vmlinuz26
default_img=kernel26.img
fallback_img=kernel26-fallback.img

install_file=kernel26.install
[ "${install_file}" != "kernel26.install" ] && cp -f $startdir/kernel26.install $startdir/${install_file}

local_version="-ARCH"
_configure_as_ARCH_way=y
# The "EXTRAVERSION" will be removed if you select _configure_as_ARCH_way as "y". 
# The final kernel version (can get it with "uname -r") as "2.6.31-ARCH"

# If you need re-configure before compile
menuconfig="n"
_include_aufs2=y
_include_squashfs=y
_with_git_patch=y
_with_next_patch=y
_with_bfs_patch=n

# Git patch
#_git_patch_url=$(links -source http://www.kernel.org/ | grep "snapshots/patch-2.6.31-.*bz2" | grep -v "?" | awk -F "\"" '{print $2}')
_git_patch_url="http://www.kernel.org/pub/linux/kernel//v2.6/snapshots/patch-2.6.31-git13.bz2"
_git_patch=$(basename ${_git_patch_url} .bz2)

# Next patch
#_next_patch_url=$(links -source http://www.kernel.org/ | grep "patch-.*next.*bz2" | grep -v "?" | awk -F "\"" '{print $2}')
_next_patch_url="http://www.kernel.org/pub/linux/kernel/v2.6/next/patch-v2.6.31-next-20090923.bz2"
_next_patch=$(basename ${_next_patch_url} .bz2)

# Base patch
_basever=2.6.31
#_base_patch_url=$(links -source http://www.kernel.org/ | grep "patch-${_basever}.[0-9].*.bz2" | grep -v "?" | awk -F "\"" '{print $2}')
_base_patch_url=""
_base_patch=$(basename ${_base_patch_url} .bz2)
######################################################

pkgname=('kernel26-base' 'kernel26-firmware' 'kernel26-source' 'kernel-headers')
pkgver=2.6.31
pkgrel=1
if [ "x${_base_patch_url}" != "x" ]; then pkgver="${_base_patch##*-}"; fi
if [ "${_with_git_patch}" = "y" ]; then pkgver="${pkgver}_${_git_patch##*-}"; fi
if [ "${_with_next_patch}" = "y" ]; then pkgver="${pkgver}_next_${_next_patch##*-}"; fi

url="http://www.kernel.org/"
arch=(i686 x86_64)
license=('GPL2')
groups=('base')
makedepends=('gcc' 'links' 'wget' 'git')
source=(config.{i686,x86_64} logo_linux_{clut224.ppm,mono.pbm,vga16.ppm}
	http://www.cn.kernel.org/pub/linux/kernel/v2.6/linux-$_basever.tar.bz2
	#http://www.kernel.org/pub/linux/kernel/v2.6/linux-$_basever.tar.bz2

	## Gentoo patches
	http://sources.gentoo.org/viewcvs.py/*checkout*/linux-patches/genpatches-2.6/trunk/2.6.31/4100_dm-bbr.patch
	http://sources.gentoo.org/viewcvs.py/*checkout*/linux-patches/genpatches-2.6/trunk/2.6.31/4202_fbcondecor-0.9.6.patch
	http://sources.gentoo.org/viewcvs.py/*checkout*/linux-patches/genpatches-2.6/trunk/2.6.31/4400_alpha-sysctl-uac.patch

	### Chinese Characters Patches http://youbest.cublog.cn/
	http://zdbr.net.cn/download/utf8-kernel-2.6.31-core-1.patch.bz2
	http://zdbr.net.cn/download/utf8-kernel-2.6-fonts-2.patch.bz2
	http://zdbr.net.cn/download/utf8-kernel-2.6.28-fbcondecor-1.patch.bz2

	# RealTime patch
	#ftp://ftp.kernel.org/pub/linux/kernel/projects/rt/patch-2.6.31-rt10.bz2

	#BFS patches
	bfs1_2.6.31-sched-bfs-232.patch::"http://ck.kolivas.org/patches/bfs/2.6.31-sched-bfs-232.patch"
	bfs2_bfs230-fixdodgydrivers2.patch::"http://ck.kolivas.org/patches/bfs/bfs230-fixdodgydrivers2.patch"
	bfs3_autoiso-xorg.patch::"http://ck.kolivas.org/patches/bfs/autoiso-xorg.patch"

	# Fedora KMS patch for nouveau
	f072_drm-next.patch::"http://cvs.fedoraproject.org/viewvc/rpms/kernel/F-12/drm-next.patch?view=co"
	f073_drm-nouveau.patch::"http://cvs.fedoraproject.org/viewvc/rpms/kernel/F-12/drm-nouveau.patch?view=co"
)

build() {

	KARCH=x86
	cd "$srcdir/linux-$_basever"

	###########################################
	###	Apply patches				##
	###########################################

	msg "Add patches..."

	msg2 "Adding Archlinux logo..."
	cp "$srcdir/logo_linux_clut224.ppm"  drivers/video/logo/ &&
	cp "$srcdir/logo_linux_mono.pbm"     drivers/video/logo/ &&
	cp "$srcdir/logo_linux_vga16.ppm"    drivers/video/logo/ || return 1

	if [ "x${_base_patch_url}" != "x" ]; then
		msg2 "Add base patch ${_base_patch}"
		wget ${_base_patch_url}
		bzip2 -d  ${_base_patch}.bz2
		patch -Np1 -i ${_base_patch} || return 1
	fi

	if [ "x${_with_git_patch}" = "y" ]; then
		msg2 "Add git patch ${_git_patch} ..."
		wget ${_git_patch_url}
		bzip2 -d  ${_git_patch}.bz2
		patch -Np1 -i ${_git_patch}
	fi
	if [ "${_with_next_patch}" = "y" ]; then
		msg2 "Add next patch ${_next_patch} ..."
		wget ${_next_patch_url}
		bzip2 -d  ${_next_patch}.bz2
		patch -Np1 -i ${_next_patch}
	fi

	msg2 "Add Gentoo patches ..."
	for i in $(ls $srcdir/4[0-9][0-9][0-9]*);do
    		echo "Applying $(basename $i)" 
  		patch -Np1 -i $i || return 1
	done

	msg2 "Add the kernel Chinese characters patches http://youbest.cublog.cn/"
	patch -Np1 -i $srcdir/utf8-kernel-2.6.31-core-1.patch || return 1
	patch -Np1 -i $srcdir/utf8-kernel-2.6.28-fbcondecor-1.patch || return 1
	patch -Np1 -i $srcdir/utf8-kernel-2.6-fonts-2.patch || return 1

#	msg2 "Adding reiserfs4 patch reiser4-for-${_basever}.patch"
#	patch -Np1 -i $srcdir/reiser4-for-${_basever}.patch || return 1

	if [ "${_include_aufs2}" = "y" ]; then 
		msg2 "Fetch the aufs2-standalone git tree..."
		if [ -d $startdir/aufs2-standalone ]; then
			cd $startdir/aufs2-standalone
			git pull origin
		else
			git clone http://git.c3sl.ufpr.br/pub/scm/aufs/aufs2-standalone.git $startdir/aufs2-standalone || return 1
		fi
		[ -d $srcdir/aufs2-standalone ] && rm -rf $srcdir/aufs2-standalone
		cp -a $startdir/aufs2-standalone $srcdir/aufs2-standalone
		pushd $srcdir/aufs2-standalone
		git checkout origin/aufs2
		popd
		cp -av $srcdir/aufs2-standalone/{Documentation,fs,include,aufs2-base.patch,aufs2-kbuild.patch,aufs2-standalone.patch} $srcdir/linux-$_basever/
		cd $srcdir/linux-$_basever
		msg2 "Adding aufs2 patches..."
		patch -Np1 -i aufs2-kbuild.patch #|| return 1
		patch -Np1 -i aufs2-base.patch #|| return 1
		patch -Np1 -i aufs2-standalone.patch #|| return 1
	fi
#	msg2 "Apply RealTime patches..."
#	patch -Np1 -i $srcdir/patch-2.6.31-rt10 #|| return 1

	if [ "${_with_bfs_patch}" = "y" ]; then
		msg2 "Apply BFS patches..."
		for i in $(ls $srcdir/bfs[1-9]*);do
			msg "Applying ${i##*/}..."
			patch -Np1 -i $i
		done
	fi

	msg2 "Add Fedora patches ..."
	for i in $(ls $srcdir/f[0-9][0-9][0-9]*);do
 		echo "Applying $(basename $i)" 
		patch -Np1 -i $i #||return 1
	done

	###########################################
	###	Configuration				##
	###########################################

	# remove extraversion
	sed -i 's|^EXTRAVERSION = .*$|EXTRAVERSION =|g' Makefile

	if [ "$CARCH" = "x86_64" ]; then
		cat ../config.x86_64 >./.config
	else
		cat ../config.i686 >./.config
	fi

	if [ "$_configure_as_ARCH_way" = "y" ]; then
		msg "Configure the kernel version as ARCH way, like 2.6.31-ARCH"
		echo "" >localversion-next
		sed -i "s|^EXTRAVERSION = .*$|EXTRAVERSION =|g" Makefile
		sed -i "s|.*CONFIG_LOCALVERSION=.*|CONFIG_LOCALVERSION=\"${local_version}\"|g" .config
		sed -i "s|.*CONFIG_LOCALVERSION_AUTO.*|\# CONFIG_LOCALVERSION_AUTO is not set|g" .config
	fi

	# The default .config without support for squashfs, can change it by set "_include_squashfs" as "y".
	if [ "$_include_squashfs" = "y" ]; then
		msg "Selected squashfs4 included in kernel package."
		if [ "x$(grep "CONFIG_SQUASHFS=m" .config)" = "x" ]; then
			sed -i "s|.*CONFIG_SQUASHFS.*|CONFIG_SQUASHFS=m\
				\n# CONFIG_SQUASHFS_EMBEDDED is not set\
				\nCONFIG_SQUASHFS_FRAGMENT_CACHE_SIZE=3|g" ./.config
		fi
	else
		if [ "x$(grep "CONFIG_SQUASHFS=m" .config)" != "x" ]; then
			sed -i "s|CONFIG_SQUASHFS=m|\# CONFIG_SQUASHFS is not set|g" .config
		fi
	fi
	if [ "${_include_aufs2}" = "y" ]; then 
		msg "Selected aufs2 included in kernel package."
		if [ "x$(grep "CONFIG_AUFS_FS=m" .config)" = "x" ]; then
			sed -i "/.*CONFIG_NILFS2_FS.*/ a\
				\CONFIG_AUFS_FS=m\
				\n# CONFIG_AUFS_BRANCH_MAX_127 is not set\
				\n# CONFIG_AUFS_BRANCH_MAX_511 is not set\
				\nCONFIG_AUFS_BRANCH_MAX_1023=y\
				\n# CONFIG_AUFS_BRANCH_MAX_32767 is not set\
				\nCONFIG_AUFS_HINOTIFY=y\
				\nCONFIG_AUFS_EXPORT=y\
				\nCONFIG_AUFS_RDU=y\
				\nCONFIG_AUFS_SHWH=y\
				\nCONFIG_AUFS_BR_RAMFS=y\
				\nCONFIG_AUFS_BR_FUSE=y\
				\n# CONFIG_AUFS_DEBUG is not set\
				\nCONFIG_AUFS_BDEV_LOOP=y\
				\nCONFIG_AUFS_POLL=y" ./.config
		fi
	else
		if [ "x$(grep "CONFIG_AUFS_FS=m" .config)" != "x" ]; then
			sed -i "s|CONFIG_AUFS_FS=m|\# CONFIG_AUFS_FS is not set|g" .config
		fi
	fi
	msg2 "Update .config Finished"

	if [ "$menuconfig" = "y" ]; then
		make menuconfig
	fi
	yes "" | make config

	###########################################
	###	Checking kernel version			##
	###########################################

	msg "Checking the kernel version..."
	make prepare
	_kernver="$(make kernelrelease)"
	if [ "$_configure_as_ARCH_way" = "y" ]; then
		# Check the _kernver again, if it is your wanted (Special for _configure_as_ARCH_way)
		_localversion=$(grep "CONFIG_LOCALVERSION=" .config | cut -d "\"" -f 2)
		if [ "${_kernver#*-}" != ${_localversion#-} ]; then
			msg "There are some errors when get your own kernel version."
			msg "_kernver=${_kernver}, _localversion=${_localversion}."
			exit 1
		else
			msg "Get the right kernel version as ${_kernver}"
		fi
	fi

	###########################################
	###	Compile					##
	###########################################

	msg "Building kernel and modules..."
	make bzImage modules || return 1
	msg "Compile finished!"
}

package_kernel26-base(){
	pkgdesc="The Linux Kernel and modules with Gentoo patches, aufs2 file system patch, \
			and kernel Chinese characters patches. "
	provides=('kernel26=2.6.31' 'kernel26=2.6.31-ARCH' 'aufs')
	conflicts=('kernel24' 'kernel24-scsi' 'kernel26-scsi' 'kernel26' 'unionfs' 'aufs')
	depends=('coreutils' 'module-init-tools' 'mkinitcpio>=0.5.15')
	if [ "${preset_file}" = "kernel26.preset" ]; then
		backup=(etc/mkinitcpio.d/kernel26.preset)
	fi
	install=${install_file}

	KARCH="x86"
	cd "$srcdir/linux-$_basever"
	_kernver="$(make kernelrelease)"

	msg "Installing kernel image..."
	install -D -m644 System.map                "$pkgdir/boot/System.map26" &&
	install -D -m644 arch/$KARCH/boot/bzImage "$pkgdir/boot/${kernel_name}" &&
	install -D -m644 .config                   "$pkgdir/boot/kconfig26"    || return 1

	msg "Installing kernel modules..."
	make INSTALL_MOD_PATH="$pkgdir" modules_install

	mkdir -p $pkgdir/etc/mkinitcpio.d

	msg "Generating kernel26.kver..."
	echo -e "# DO NOT EDIT THIS FILE\nALL_kver='$_kernver'" \
		> "$pkgdir/etc/mkinitcpio.d/${kver_file}" || return 1

	msg "Generating preset file..."
	echo -e "# mkinitcpio preset file for $pkgname\n \
		\n########################################\
		\n# DO NOT EDIT THIS LINE:\
		\nsource /etc/mkinitcpio.d/${kver_file}\
		\n########################################\
		\nALL_config=\"/etc/mkinitcpio.conf\"\
		\n\nPRESETS=('default' 'fallback')\
		\n\n#default_config=\"/etc/mkinitcpio.conf\" \
		\ndefault_image=\"/boot/${default_img}\" \
		\n#default_options=\"\" \
		\n#fallback_config=\"/etc/mkinitcpio.conf\" \
		\nfallback_image=\"/boot/${fallback_img}\" \
		\nfallback_options=\"-S autodetect\" "\
		> "$pkgdir/etc/mkinitcpio.d/${preset_file}" || return 1

	msg "Update install scripts..."
	sed -i "s%kernelname=.*%kernelname=${kernel_name}%g;\
		s%default_img=.*%default_img=${default_img}%g;\
		s%fallback_img=.*%fallback_img=${fallback_img}%g;\
		s%pacman.*grep%pacman -Ql $pkgname | grep%g;\
		s%mkinitcpio.*%mkinitcpio -p ${preset_file%.preset}%g;\
		s%kernelname=.*%kernelname="${kernel_name}"%g"\
		$startdir/${install_file} || return 1

	msg "Remove the firmware..."
	rm -rf "$pkgdir/lib/firmware"

	msg "Kernel and modules installing finished."
}

package_kernel26-firmware() {
	pkgdesc="The included firmware files of the Linux Kernel."
	provides=('kernel26-firmware' 'kernel26-firmware=2.6.31')

	cd "$srcdir/linux-$_basever"

	msg "Install the kernel26-firmware..."
	make INSTALL_MOD_PATH="$pkgdir" modules_install
	rm -rf $pkgdir/lib/modules

	msg "kernel firmware installing finished."
}

package_kernel26-source() {
	pkgdesc="Kernel source, with Gentoo patches, aufs2 filesystems patches, and Fedora KMS patches."
	provides=('kernel26-source')

	KARCH="x86"

	cd "$srcdir/linux-$_basever"
	_kernver="$(make kernelrelease)"

	msg "Installing the kernel source..."
	# Install configuration and other files...
	install -D -m644 Module.symvers  "$pkgdir/usr/src/linux-$_kernver/Module.symvers"  &&
	install -D -m644 Makefile        "$pkgdir/usr/src/linux-$_kernver/Makefile"        &&
	install -D -m644 kernel/Makefile "$pkgdir/usr/src/linux-$_kernver/kernel/Makefile" &&
	install -D -m644 .config         "$pkgdir/usr/src/linux-$_kernver/.config"         &&
	install -D -m644 .config         "$pkgdir/lib/modules/$_kernver/.config"           || return 1

	# Install scripts directory and fix permissions on it
	cp -a scripts "$pkgdir/usr/src/linux-$_kernver" &&
		chmod og-w -R "$pkgdir/usr/src/linux-$_kernver" || return 1

	msg "Installing header files..."
	# kernel headers
	msg2 "kernel"
	for i in acpi asm-{generic,$KARCH} config linux math-emu media net pcmcia scsi sound trace video; do
		mkdir -p "$pkgdir/usr/src/linux-$_kernver/include/$i" &&
			cp -a include/$i "$pkgdir/usr/src/linux-$_kernver/include" || return 1
	done

	# lirc headers
	msg2 "lirc"
	mkdir -p "$pkgdir/usr/src/linux-$_kernver/drivers/media/video" &&
		cp drivers/media/video/*.h "$pkgdir/usr/src/linux-$_kernver/drivers/media/video/" || return 1

	for i in bt8xx cpia2 cx25840 cx88 em28xx et61x251 pwc saa7134 sn9c102 usbvideo zc0301; do
		mkdir -p "$pkgdir/usr/src/linux-$_kernver/drivers/media/video/$i" &&
			cp -a drivers/media/video/$i/*.h "$pkgdir/usr/src/linux-$_kernver/drivers/media/video/$i" || return 1
	done

	# md headers
	msg2 "md"
	mkdir -p "$pkgdir/usr/src/linux-$_kernver/drivers/md" &&
		cp drivers/md/*.h "$pkgdir/usr/src/linux-$_kernver/drivers/md" || return 1

	# inotify.h
	msg2 "inotify.h"
	mkdir -p "$pkgdir/usr/src/linux-$_kernver/include/linux" &&
		cp include/linux/inotify.h "$pkgdir/usr/src/linux-$_kernver/include/linux/" || return 1

	# CLUSTERIP file for iptables
	msg2 "CLUSTERIP file for iptables"
	mkdir -p "$pkgdir/usr/src/linux-$_kernver/net/ipv4/netfilter/" &&
		cp net/ipv4/netfilter/ipt_CLUSTERIP.c "$pkgdir/usr/src/linux-$_kernver/net/ipv4/netfilter/" || return 1

	# wireless headers
	msg2 "wireless"
	mkdir -p "$pkgdir/usr/src/linux-$_kernver/net/mac80211/" &&
		cp net/mac80211/*.h "$pkgdir/usr/src/linux-$_kernver/net/mac80211/" || return 1

	# Kconfig files
	msg2 "Kconfig files"
	for i in $(find . -name "Kconfig*"); do
		mkdir -p "$pkgdir/usr/src/linux-$_kernver/${i%/*}" &&
			cp "$i" "$pkgdir/usr/src/linux-$_kernver/$i" || return 1
	done

	# Install architecture dependent files
	msg "Installing architecture files..."
	mkdir -p "$pkgdir/usr/src/linux-$_kernver/arch/$KARCH/kernel" &&
		cp arch/$KARCH/kernel/asm-offsets.s "$pkgdir/usr/src/linux-$_kernver/arch/$KARCH/kernel"

	cp arch/$KARCH/Makefile* "$pkgdir/usr/src/linux-$_kernver/arch/$KARCH"
	cp -r arch/$KARCH/configs "$pkgdir/usr/src/linux-$_kernver/arch/$KARCH"

	# copy arch includes for external modules and fix the nVidia issue
	mkdir -p "$pkgdir/usr/src/linux-$_kernver/arch/$KARCH" &&
		cp -a "arch/$KARCH/include" "$pkgdir/usr/src/linux-$_kernver/arch/$KARCH/" || return 1

	# create a necessary symlink to the arch folder
	cd "$pkgdir/usr/src/linux-$_kernver/arch" || return 1

	if [ "$CARCH" = "x86_64" ]; then
		ln -s $KARCH x86_64 || return 1
	else
		ln -s $KARCH i386 || return 1
	fi

	cd "$OLDPWD" || return 1

	msg "Removing unneeded architectures..."
	for i in "$pkgdir/usr/src/linux-$_kernver/arch/"*; do
		[ "${i##*/}" != "$KARCH" ] && rm -rf "$i"
	done

	msg "Creating important symlinks..."
	cd "$pkgdir/usr/src/linux-$_kernver/include" &&
		ln -s asm-$KARCH asm &&
		cd "$OLDPWD" || return 1

	mkdir -p $pkgdir/lib/modules/$_kernver
	cd "$pkgdir/lib/modules/$_kernver" &&
		rm -rf source build &&
		ln -s ../../../usr/src/linux-$_kernver build &&
		cd "$OLDPWD" || return 1

	msg "Fixing permissions..."
	chown -R root:root "$pkgdir/usr/src/linux-$_kernver" &&
		find "$pkgdir/usr/src/linux-$_kernver" -type d -exec chmod 755 {} \; || return 1

	msg "Kernel source installing finished."
}
package_kernel-headers() {
	pkgdesc="Kernel headers sanitized for use in userspace."
	provides=('kernel-headers' 'kernel-headers=2.6.31')

	cd "$srcdir/linux-$_basever"
	_kernver="$(make kernelrelease)"

	msg "Installing the kernel headers..."
	#make mrproper || return 1
	#make headers_check || return 1
	make INSTALL_HDR_PATH="${pkgdir}/usr" headers_install || return 1
	rm -rf "${pkgdir}/usr/include/drm"
	#find ${pkgdir} -type f -name "*.cmd" -exec rm -rf {}\;

#	msg "Updating pkgver to ${_kernver%-*}..."
#	pkgver=${_kernver%-*}

	if [ "$_keep_build_dir" != "y" ]; then
		# Remove build directory
		rm -rf "$srcdir/linux-$_basever"
	fi
	msg "kernel headers installed finished."
}
